package cn.lirenyi.org.controller;

import cn.lirenyi.basic.exception.BusinessException;
import cn.lirenyi.basic.util.AjaxResult;
import cn.lirenyi.basic.util.PageList;
import cn.lirenyi.org.domain.Shop;
import cn.lirenyi.org.query.ShopQuery;
import cn.lirenyi.org.service.IShopService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细功能")
public class ShopController {

    @Autowired
    private IShopService service;

    @GetMapping("/{id}")    // 相当于@RequestMapping(value="/id",method = RequestMethod.GET)
    // 根据id查
    public Shop queryById(@PathVariable("id") Long id){
        return service.queryById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        // 先判断有无id，进而判断是添加还是修改方法
        try {
            // 没有id就是添加
            if (shop.getId() == null){
                service.add(shop);
            } else {
                // 有id就是修改
                service.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }

    }

    @DeleteMapping("/{id}")     // 删除
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("艸作失败");
        }
    }

    @PostMapping    // 分页查询
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        PageList<Shop> page = service.queryPage(shopQuery);
        System.out.println(page);
        return page;
    }

    @GetMapping    // 查全部
    public List<Shop> queryAll(){
        return service.queryAll();
    }


    @PatchMapping     // 批量删除
    public AjaxResult patchDel(@RequestBody Long[] id){
        try {
            service.patchDel(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("艸作失败");
        }
    }

    @PostMapping("/settlement")     // 店铺入驻
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            service.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后再试");
        }
    }






}
